package com.cocostudio.erpapp;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Spinner;
import android.widget.TextView;

import com.cocostudio.erpapp.adapter.DeptmentAdapter;
import com.cocostudio.erpapp.common.Debug;
import com.cocostudio.erpapp.utils.Constant;
import com.cocostudio.erpapp.utils.UIHelper;
import com.cocostudio.erpapp.utils.WebServiceUtils;
import com.cocostudio.erpapp.vo.ErpDeptment;
import com.cocostudio.erpapp.web.CustomWebChromeClient;
import com.google.zxing.client.android.CaptureActivity;


public class RealTimeStockActivity extends CustomFragmentActivity {
    private static final String TAG = "RealTimeStockActivity";
    private static View searchView;

    private WebView webView;
    private String showtype;
    private int deptlevel;
    private String deptid;
    private String categorylevel;

    private EditText search_product_name;
    private EditText search_product_size;
    private Spinner search_dept_code_spinner;
    private Spinner search_showtype_spinner;

    //private EditText search_dept_level;
    private EditText search_product_code;
    private EditText search_product_barcode;

    private LinearLayout cell_category_level;
    private LinearLayout cell_dept_code;
    private LinearLayout cell_dept_level;
    private LinearLayout cell_product;
    private LinearLayout cell_barcode;




    //private RadioGroup search_showtype;
    private RadioGroup search_dept_level;
    private RadioGroup search_category_level;


    public static final int SCAN_CODE = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        if (Debug.LOGD) {
            Log.d(TAG, "onCreate.");
        }
        super.onCreate(savedInstanceState);

        initView();
    }

    public void onScanBarCode(View view){
        Intent intent = new Intent(RealTimeStockActivity.this, CaptureActivity.class);
        startActivityForResult(intent, SCAN_CODE);
    }
    public void onSearchClicked(View view) {
        
        if ( this.showtype.equals("4") ){
            if ( (search_product_barcode.getText().toString().trim().length()==0) &&
                    (search_product_code.getText().toString().trim().length()==0)   
                    )
            {
                UIHelper.showAlert(this,"请确认商品信息输入正确");
                search_product_code.setFocusable(true);
                return;
            }
        }
        
        try {
            pdg = ProgressDialog.show(RealTimeStockActivity.this, "温馨提示", "数据请求中，请稍后！");
            pdg.setCancelable(true);
            pdg.setCanceledOnTouchOutside(true);


            webView.loadUrl( Constant.urlprefix+ "realtime_stock.html");
            pdg.dismiss();

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
        case SCAN_CODE:
            TextView scanResult = (TextView) findViewById(R.id.search_product_barcode);
            if (resultCode == RESULT_OK) {
                String result = data.getStringExtra("scan_result");
                scanResult.setText(result);
                onSearchClicked(null);
            } else if (resultCode == RESULT_CANCELED) {
                scanResult.setText("");
            }
            break;
        default:
            break;
        }
    }


    @SuppressLint("SetJavaScriptEnabled")
	private void initView() {

        this.showtype = "1";
        this.deptlevel=1;
        this.categorylevel = "1";
        this.deptid = "01";
        setContentView(R.layout.activity_realtime_stock);

        setTitle(R.string.title_realtime_store);

        setLeftButtonDrawable(R.drawable.btn_home);

        searchView = findViewById(R.id.search_component);

        search_dept_code_spinner= (Spinner)findViewById(R.id.search_dept_code);
        search_product_code= (EditText)findViewById(R.id.search_product_code);
         search_product_barcode=(EditText)findViewById(R.id.search_product_barcode);

         cell_dept_code  = (LinearLayout)findViewById(R.id.ui_cell_dept_code);
         cell_product = (LinearLayout)findViewById(R.id.ui_cell_product_code );
         cell_barcode = (LinearLayout)findViewById(R.id.ui_cell_barcode);
         
         cell_category_level = (LinearLayout)findViewById(R.id.ui_cell_realtime_category_level);
         cell_dept_level = (LinearLayout)findViewById(R.id.ui_cell_realtime_dept_level);
         
         search_dept_level = (RadioGroup)findViewById(R.id.deptlevel_radiogroup);

        initShowTypeUI();
        initDeptUI();
        initCategoryUI();
        initWebViewUI();

        search_product_code.setText( Constant.curProductCode );

   }

    private void initWebViewUI() {
        webView = (WebView) findViewById(R.id.detail_result_webview);

        webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
        webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        webView.getSettings().setBuiltInZoomControls(true);
        webView.getSettings().setSupportZoom(false);
        webView.setFocusableInTouchMode(true);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebChromeClient( new CustomWebChromeClient());
        webView.addJavascriptInterface(this, "android");
    }

    private void initCategoryUI() {
        search_category_level = (RadioGroup)findViewById(R.id.categorylevel_radiogroup);
        search_category_level.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {

                int btnid = group.getCheckedRadioButtonId();
                switch (btnid) {
                case R.id.categorylevel1:
                   RealTimeStockActivity.this.categorylevel ="1";
                    break;
                case R.id.categorylevel2:
                    RealTimeStockActivity.this.categorylevel="2";
                    break;
                case R.id.categorylevel3:
                    RealTimeStockActivity.this.categorylevel="3";
                    break;
                case R.id.categorylevel4:
                    RealTimeStockActivity.this.categorylevel="4";
                    break;
                default:
                    break;
                }
            }
        });
    }

    private void initShowTypeUI() {
        search_showtype_spinner = (Spinner)findViewById(R.id.search_showtype_spinner);
        String[] mshowItems = new String[]{"分部门","分品牌","分类别","单品","分供货商"};


        ArrayAdapter<String> _Adapter_showtype=new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line, mshowItems);
        search_showtype_spinner.setAdapter(_Adapter_showtype);
        search_showtype_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                RealTimeStockActivity.this.showtype = Integer.toString(position+1);
                Log.d(RealTimeStockActivity.TAG, "select position:"+position);

                //3+1==按单品
                if( 4 == (position+1) ){

                    RealTimeStockActivity.this.cell_dept_code.setVisibility(View.GONE);
                    RealTimeStockActivity.this.cell_dept_level.setVisibility(View.GONE);
                    RealTimeStockActivity.this.cell_category_level.setVisibility(View.GONE);
                    
                    RealTimeStockActivity.this.cell_product.setVisibility(View.VISIBLE);
                    RealTimeStockActivity.this.cell_barcode.setVisibility(View.VISIBLE);
     
                    
                    Log.d(RealTimeStockActivity.TAG, "单品 showtype .........");

                }
                //按类别
                else if( 3==(position+1))
                {
                    RealTimeStockActivity.this.cell_product.setVisibility(View.GONE);
                    RealTimeStockActivity.this.cell_barcode.setVisibility(View.GONE);
                    RealTimeStockActivity.this.cell_dept_code.setVisibility(View.VISIBLE);
                    RealTimeStockActivity.this.cell_dept_level.setVisibility(View.VISIBLE);
                    RealTimeStockActivity.this.cell_category_level.setVisibility(View.VISIBLE);
                    Log.d(RealTimeStockActivity.TAG, "类别 showtype .........");

                }
                //其他
                else 
                {
                    RealTimeStockActivity.this.cell_product.setVisibility(View.GONE);
                    RealTimeStockActivity.this.cell_barcode.setVisibility(View.GONE);
                    RealTimeStockActivity.this.cell_category_level.setVisibility(View.GONE);
                    
                    RealTimeStockActivity.this.cell_dept_code.setVisibility(View.VISIBLE);
                    RealTimeStockActivity.this.cell_dept_level.setVisibility(View.VISIBLE);
                    
                    Log.d(RealTimeStockActivity.TAG, "Other ShowType.........");
                }
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent){
                Log.d(RealTimeStockActivity.TAG, "select onNothing");
            }
        });
    }

    private void initDeptUI() {
        
        search_dept_code_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                   ErpDeptment item = (ErpDeptment) search_dept_code_spinner.getItemAtPosition(position);
                   RealTimeStockActivity.this.deptid=item.getCode();
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent){
                Log.d(RealTimeStockActivity.TAG, "select onNothing");
            }
        });
        
        

        if( ! WebServiceUtils.checkHasDeptLevel(1)){
            ((RadioButton)findViewById(R.id.deptlevel1)).setVisibility(View.GONE);
            this.deptlevel=2;
            ((RadioButton)findViewById(R.id.deptlevel2)).setChecked(true);
        }
        if( ! WebServiceUtils.checkHasDeptLevel(2)){
            ((RadioButton)findViewById(R.id.deptlevel2)).setVisibility(View.GONE);
            this.deptlevel=3;
            ((RadioButton)findViewById(R.id.deptlevel3)).setChecked(true);

        }
        if( ! WebServiceUtils.checkHasDeptLevel(3)){
            ((RadioButton)findViewById(R.id.deptlevel3)).setVisibility(View.GONE);
            this.deptlevel=4;
        }
        if( ! WebServiceUtils.checkHasDeptLevel(4))
            ((RadioButton)findViewById(R.id.deptlevel4)).setVisibility(View.GONE);
        
        
        search_dept_level.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {

                int btnid = group.getCheckedRadioButtonId();
                switch (btnid) {
                case R.id.deptlevel1:
                   RealTimeStockActivity.this.deptlevel=1;
                    break;
                case R.id.deptlevel2:
                    RealTimeStockActivity.this.deptlevel=2;
                    break;
                case R.id.deptlevel3:
                    RealTimeStockActivity.this.deptlevel=3;
                    break;
                case R.id.deptlevel4:
                    RealTimeStockActivity.this.deptlevel=4;
                    break;
                default:
                    break;
                }
                
                DeptmentAdapter deptAdapter = new DeptmentAdapter(RealTimeStockActivity.this, WebServiceUtils.buildDeptList(  
                          RealTimeStockActivity.this.deptlevel ));

                search_dept_code_spinner.setAdapter(deptAdapter);
            }
        });
        
        if (this.deptlevel==1)
            ((RadioButton)findViewById(R.id.deptlevel1)).setChecked(true);
        if (this.deptlevel==2)
            ((RadioButton)findViewById(R.id.deptlevel2)).setChecked(true);
        if (this.deptlevel==3)
            ((RadioButton)findViewById(R.id.deptlevel3)).setChecked(true);
        if (this.deptlevel==4)
            ((RadioButton)findViewById(R.id.deptlevel4)).setChecked(true);
        
        DeptmentAdapter deptAdapter1 = new DeptmentAdapter(RealTimeStockActivity.this, WebServiceUtils.buildDeptList(  
                RealTimeStockActivity.this.deptlevel ));

      search_dept_code_spinner.setAdapter(deptAdapter1);
    }

    @JavascriptInterface
    public String getjson() {
        String json="";
        json= WebServiceUtils.WS_RealTimeStock_json(
                this.deptid,
                Integer.toString( this.deptlevel),
                this.categorylevel,
                search_product_code.getText().toString(),
                search_product_barcode.getText().toString(),
                this.showtype
                );
        return json;
    }

    @JavascriptInterface
    public String showtype() {
        return this.showtype;
    }
}
